package com.xbfinance.portal.service.impl;


import com.xbfinance.portal.dao.P2pUserinfoMapper;
import com.xbfinance.portal.dao.UserCustomMapper;
import com.xbfinance.portal.pojo.po.P2pUser;
import com.xbfinance.portal.pojo.po.P2pUserExample;
import com.xbfinance.portal.pojo.po.P2pUserinfo;
import com.xbfinance.portal.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserCustomMapper userDao;
    @Autowired
    private P2pUserinfoMapper userinfoDao;

    @Override
    public boolean isNameExist(String username) {
        P2pUserExample example = new P2pUserExample();
        example.createCriteria()
                .andUsernameEqualTo(username);
        List<P2pUser> userList = userDao.selectByExample(example);
        if (userList != null && userList.size() != 0)
            return true;
        return false;
    }

    @Override
    public boolean isEmailExist(String email) {
        P2pUserExample example = new P2pUserExample();
        example.createCriteria()
                .andEmailEqualTo(email);
        List<P2pUser> userList = userDao.selectByExample(example);
        if (userList != null && userList.size() != 0)
            return true;
        return false;
    }

    @Override
    public P2pUser regist(P2pUser p2pUser, String registerip) {
        p2pUser.setRegistertime(new Date());
        p2pUser.setRegisterip(registerip);
       new PasswordHelper().encryptPassword(p2pUser);
        System.out.println("11111111111"+p2pUser.getLoginpass());
        Integer userid = userDao.insertUserSelective(p2pUser);
        if (userid != null) {
            return p2pUser;
        }
        return null;
    }

    @Override
    public boolean putInfo(String realname, String cardnum, Integer userid) {
        P2pUser p2pUser = new P2pUser();
        p2pUser.setUserid(userid);
        p2pUser.setEmailcheck(true);
        p2pUser.setPhonecheck(true);
        p2pUser.setRealname(realname);
        P2pUserinfo userinfo = new P2pUserinfo();
        userinfo.setUserid(userid);
        userinfo.setCardnum(cardnum);
        boolean flag = false;
        try {
            userDao.updateByPrimaryKeySelective(p2pUser);
            userinfoDao.insertSelective(userinfo);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public void checkEmailPhone(Integer userid) {
        P2pUser p2pUser = new P2pUser();
        p2pUser.setUserid(userid);
        p2pUser.setEmailcheck(true);
        p2pUser.setPhonecheck(true);
        userDao.updateByPrimaryKeySelective(p2pUser);
    }

    @Override
    public P2pUser login(String email, String loginpass) {
        P2pUser user=new P2pUser();
        user.setLoginpass(loginpass);
        new PasswordHelper().encryptPassword(user);
        System.out.println("222222222"+user.getLoginpass());
        P2pUserExample example = new P2pUserExample();
        example.createCriteria()
                .andEmailEqualTo(email)
                .andLoginpassEqualTo(user.getLoginpass());
        List<P2pUser> userList = userDao.selectByExample(example);
        if (userList.size() != 0)
            return userList.get(0);
        return null;
    }

    @Override
    public void sendCheckEmail(P2pUser user) {
        EmailUtils.sendAccountActivateEmail(user);
    }

    @Override
    public P2pUser checkPaypass(Integer userid, String paypass) {

        P2pUserExample example = new P2pUserExample();

        example.createCriteria().andUseridEqualTo(userid).andPaypassEqualTo(paypass);

        List<P2pUser> p2pUsers = userDao.selectByExample(example);

        return p2pUsers.get(0);
    }
}
